package com.mymail.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mymail.entity.Contacts;

public class ContactsDAO extends DAOImp implements DAO {

	@SuppressWarnings("unchecked")
	public ArrayList<Contacts> getContactsByGroupId(Integer group_id,
			Integer start, Integer end) {
		String sql = "select * from `contacts` where contacts_id=? limit ?,?";
		return findArray(sql, new Object[] { group_id, start, end });
	}

	public boolean getByID(Integer id, Object obj) {
		Contacts contacts = (Contacts) obj;
		String sql = "select * from `contacts` where contacts_id=?";
		return findOne(sql, new Object[] { id }, contacts);
	}

	public boolean addNew(Object obj) {
		Contacts contacts = (Contacts) obj;
		String sql = "insert into contacts (contacts_name,contacts_email,"
				+ "contacts_gender,contacts_lasttime,contacts_count,"
				+ "contacts_birth,contacts_qq,contacts_phone,contacts_address,"
				+ "contacts_company,group_id) values (?,?,?,?,?,?,?,?,?,?,?)";
		int i = insert(sql,
				new Object[] { contacts.getName(), contacts.getEmail(),
						new String(new char[] { contacts.getGender() }),
						contacts.getLasttime(), contacts.getCount(),
						contacts.getBirth(), contacts.getQQ(),
						contacts.getPhone(), contacts.getAddress(),
						contacts.getCompany(), contacts.getId() });
		if (i > 0) {
			contacts.setId(i);
			return true;
		}
		return false;
	}

	public boolean delete(Object obj) {
		Contacts contacts = (Contacts) obj;
		String sql = "delete from contacts where contacts_id=?";
		return exe(sql, new Object[] { contacts.getId() });
	}

	public boolean toDatabase(Object obj) {
		Contacts contacts = (Contacts) obj;
		if(contacts.getId()==null){
			return addNew(contacts);
		} else {
			return update(contacts);
		}
	}

	public boolean update(Object obj) {
		Contacts contacts = (Contacts) obj;
		String sql = "update contacts set contacts_name=?,contacts_email=?,"
				+ "contacts_gender=?,contacts_lasttime=?,contacts_count=?,"
				+ "contacts_birth=?,contacts_qq=?,contacts_phone=?,contacts_address=?,"
				+ "contacts_company=? where group_id=?";
		return exe(sql,
				new Object[] { contacts.getName(), contacts.getEmail(),
						new String(new char[] { contacts.getGender() }),
						contacts.getLasttime(), contacts.getCount(),
						contacts.getBirth(), contacts.getQQ(),
						contacts.getPhone(), contacts.getAddress(),
						contacts.getCompany(), contacts.getId() });
	}

	protected Object getEntity(ResultSet result) throws SQLException {
		Contacts contacts = new Contacts();
		setAtribute(result, contacts);
		return contacts;
	}

	protected void setAtribute(ResultSet result, Object obj)
			throws SQLException {
		Contacts contacts = (Contacts) obj;
		contacts.setId(result.getInt("contacts_id"));
		contacts.setName(result.getString("contacts_name"));
		contacts.setEmail(result.getString("contacts_email"));
		contacts.setGender((char) result.getString("contacts_gender")
				.getBytes()[0]);
		contacts.setLasttime(result.getDate("contacts_lasttime"));
		contacts.setCount(new Integer(result.getInt("contacts_count")));
		contacts.setBirth(result.getDate("contacts_birth"));
		contacts.setQQ(result.getString("contacts_qq"));
		contacts.setPhone(result.getString("contacts_phone"));
		contacts.setAddress(result.getString("contacts_address"));
		contacts.setCompany(result.getString("contacts_company"));
		contacts.setGroup(result.getInt("folder_id"));
	}

}
